<?php
class FavoritesController extends AppController {

	var $name = 'Favorites';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Favorite->recursive = 0;
		$this->set('favorites', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Favorite', true), array('action'=>'index'));
		}
		$this->set('favorite', $this->Favorite->read(null, $id));
	}

	function add() {
		$this->edit();
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Favorite->create();
		}
		if (!empty($this->data)) {
			if ($this->Favorite->save($this->data)) {
				$this->flash(__('The Favorite has been saved.', true), array('action'=>'index'));
			} else {
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Favorite->read(null, $id);
			if(isset($this->params['named']['title']) && !$id) {
				$this->data['Favorite']['title'] = trim(preg_replace(array('/\s*\b(\d{1,2}x|[sS]\d{1,2}[eE])\d{2}\b.*/','/\./','/\_/'),array('',' ',' '),$this->params['named']['title']));
				$this->data['Favorite']['regex'] = trim(preg_replace(array(
					'/^/',
					'/\s*\b(\d{1,2}x|[sS]\d{1,2}[eE])\d{2}\b.*/',
					'/\./',
					'/\_/'),
					array('^','.',' '),$this->params['named']['title']));
			}
		}

		$feeds = $this->Favorite->Feed->find('list');
		$this->set(compact('feeds'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Favorite', true), array('action'=>'index'));
		}
		if ($this->Favorite->del($id)) {
			$this->flash(__('Favorite deleted', true), array('action'=>'index'));
		}
	}

}
?>